<?php

require_once __DIR__ . '/vendor/autoload.php';

$app = require_once __DIR__ . '/bootstrap/app.php';
$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();

try {
    echo "🚀 开始创建基础数据...\n";

    // 1. 创建管理员角色
    $adminRole = \DB::table('roles')->insertGetId([
        'name' => '系统管理员',
        'code' => 'admin',
        'description' => '系统管理员，拥有所有权限',
        'status' => 'active',
        'created_at' => now(),
        'updated_at' => now(),
    ]);
    echo "✅ 创建管理员角色 ID: $adminRole\n";

    // 2. 创建菜单数据
    $menus = [
        [
            'parent_id' => null,
            'name' => '仪表板',
            'code' => 'dashboard',
            'icon' => 'dashboard',
            'path' => '/dashboard',
            'component' => 'Dashboard',
            'type' => 'menu',
            'description' => '系统仪表板',
            'permissions' => json_encode([]),
            'status' => 'active',
            'is_external' => false,
            'is_visible' => true,
            'sort_order' => 1,
            'created_at' => now(),
            'updated_at' => now(),
        ],
        [
            'parent_id' => null,
            'name' => '系统管理',
            'code' => 'system',
            'icon' => 'setting',
            'path' => '/system',
            'component' => null,
            'type' => 'menu',
            'description' => '系统管理模块',
            'permissions' => json_encode(['system.manage']),
            'status' => 'active',
            'is_external' => false,
            'is_visible' => true,
            'sort_order' => 2,
            'created_at' => now(),
            'updated_at' => now(),
        ],
        [
            'parent_id' => null,
            'name' => '人事管理',
            'code' => 'hr',
            'icon' => 'team',
            'path' => '/hr',
            'component' => null,
            'type' => 'menu',
            'description' => '人事管理模块',
            'permissions' => json_encode(['hr.manage']),
            'status' => 'active',
            'is_external' => false,
            'is_visible' => true,
            'sort_order' => 3,
            'created_at' => now(),
            'updated_at' => now(),
        ],
    ];

    $menuIds = [];
    foreach ($menus as $menu) {
        $id = \DB::table('menus')->insertGetId($menu);
        $menuIds[$menu['code']] = $id;
        echo "✅ 创建菜单: {$menu['name']} (ID: $id)\n";
    }

    // 创建子菜单
    $subMenus = [
        [
            'parent_id' => $menuIds['system'],
            'name' => '用户管理',
            'code' => 'system.users',
            'icon' => 'user',
            'path' => '/system/users',
            'component' => 'system/Users',
            'type' => 'menu',
            'description' => '用户管理',
            'permissions' => json_encode(['user.manage']),
            'status' => 'active',
            'is_external' => false,
            'is_visible' => true,
            'sort_order' => 1,
            'created_at' => now(),
            'updated_at' => now(),
        ],
        [
            'parent_id' => $menuIds['system'],
            'name' => '角色管理',
            'code' => 'system.roles',
            'icon' => 'usergroup-add',
            'path' => '/system/roles',
            'component' => 'system/Roles',
            'type' => 'menu',
            'description' => '角色管理',
            'permissions' => json_encode(['role.manage']),
            'status' => 'active',
            'is_external' => false,
            'is_visible' => true,
            'sort_order' => 2,
            'created_at' => now(),
            'updated_at' => now(),
        ],
        [
            'parent_id' => $menuIds['hr'],
            'name' => '员工管理',
            'code' => 'hr.employees',
            'icon' => 'user',
            'path' => '/hr/employees',
            'component' => 'hr/Employees',
            'type' => 'menu',
            'description' => '员工管理',
            'permissions' => json_encode(['employee.manage']),
            'status' => 'active',
            'is_external' => false,
            'is_visible' => true,
            'sort_order' => 1,
            'created_at' => now(),
            'updated_at' => now(),
        ],
        [
            'parent_id' => $menuIds['hr'],
            'name' => '部门管理',
            'code' => 'hr.departments',
            'icon' => 'apartment',
            'path' => '/hr/departments',
            'component' => 'hr/Departments',
            'type' => 'menu',
            'description' => '部门管理',
            'permissions' => json_encode(['department.manage']),
            'status' => 'active',
            'is_external' => false,
            'is_visible' => true,
            'sort_order' => 2,
            'created_at' => now(),
            'updated_at' => now(),
        ],
    ];

    foreach ($subMenus as $menu) {
        $id = \DB::table('menus')->insertGetId($menu);
        echo "✅ 创建子菜单: {$menu['name']} (ID: $id)\n";
    }

    // 3. 将用户关联到管理员角色
    $user = \DB::table('users')->first();
    if ($user) {
        \DB::table('user_roles')->insert([
            'user_id' => $user->id,
            'role_id' => $adminRole,
            'created_at' => now(),
            'updated_at' => now(),
        ]);
        echo "✅ 将用户 {$user->username} 关联到管理员角色\n";
    }

    echo "🎉 基础数据创建完成！\n";

} catch (Exception $e) {
    echo "❌ 错误：" . $e->getMessage() . "\n";
    echo "📍 文件：" . $e->getFile() . " 行：" . $e->getLine() . "\n";
}
